package com.fast.bigdata.hourglass.config;

import bigdata.hourglass.exeception.HourglassException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization CC
 * @website https://www.jlpyyf.com
 * @date 2019/12/11 16:29
 * @since 1.0
 */
@Component
public class EnvConfig {
    @Autowired
    private Environment env;

    private boolean propertyNotFound(String key) {
        return !env.containsProperty(key);
    }

    public String getProperty(String key) {
        if (propertyNotFound(key)) {
            throw new HourglassException(">>>>> config [" + key + "] not exists.");
        }
        return env.getProperty(key);
    }

    public String getProperty(String key, String defaultValue) {
        if (propertyNotFound(key)) {
            return defaultValue;
        }
        String value = env.getProperty(key);
        if (value == null || value.trim().length() == 0) {
            return defaultValue;
        }
        return env.getProperty(key);
    }
}
